//
//  QuizAnalyView.m
//  SubmitVCDemo
//
//  Created by Tianlong on 2018/1/24.
//  Copyright © 2018年 Tianlong. All rights reserved.
//

#import "QuizAnalyView.h"
#import "QuizAnalysisCell.h"

#define ItemM 20    // item间距
#define ItemW 47    // item的按钮
#define OtherViewH  66  // 除开contentView的高度剩下的高度 66
#define ContentViewLeftRightM 30
@interface QuizSubmitAnalysisViewFlowlayout:UICollectionViewFlowLayout

@end
@implementation QuizSubmitAnalysisViewFlowlayout
@end

@interface QuizAnalyView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHeightC;

@property (weak, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end

@implementation QuizAnalyView
static NSString *cellId = @"QuizAnalysisCell";

- (void)awakeFromNib{
    [super awakeFromNib];
}

#pragma mark - 快速创建
+ (instancetype)viewWithDataSource:(NSArray *)dataSource{
    QuizAnalyView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([QuizAnalyView class]) owner:self options:nil] lastObject];
    view.dataSource = [NSMutableArray arrayWithArray:dataSource];
    
    for (int i = 0; i < TestCount; i++) {
        QuizAnalyListItem *item = [[QuizAnalyListItem alloc] init];
        item.itemWidth = ItemW;
        item.itemHeight = ItemW;
        item.number = [NSString stringWithFormat:@"%d",i];
        if(i%3==0){
            item.title = @"正确";
            item.type = @"1";
        }else if(i%4==0){
            item.title = @"待修改";
            item.type = @"2";
        }else{
            item.title = @"待批改";
            item.type = @"3";
        }
        [view.dataSource addObject:item];
    }
    if(view.dataSource.count > 0){
        [view.collectionView reloadData];
    }
    return view;
}

#pragma mark - 计算collectionview的真实高度
+ (CGFloat)getCollRealHeightWithDataSourceCount:(NSInteger)count{
    // 计算列数和行数?
    CGFloat collH = 0;
    NSUInteger cols = 0;
    NSUInteger rows = 0;
    if (count > 0) {
        cols = (NSUInteger)(([UIScreen mainScreen].bounds.size.width - ContentViewLeftRightM*2-ItemW) / (ItemW+ItemM) + 1);
        cols = cols > count ? count : cols;
        if (cols > 0) {
            rows = (NSUInteger)(count / cols);
            if(count > cols){
                if(count%cols == 0){
                    rows = count/cols;
                }else{
                    if((count%cols) > 0) {
                        rows = count/cols + 1;
                    }
                }
            }else{
                rows = 1;
            }
        }
    }
    
    if (rows > 1) {
        collH = ItemW + (rows-1)*(ItemW+ItemM);
    }else{
        collH = ItemW;
    }
    NSLog(@"rows = %ld cols = %ld collH = %.0f realH = %.0f",rows,cols,collH,collH+OtherViewH);
    return collH;
}

#pragma mark - 获取真实高度
+ (CGFloat)getRealHeightWithDataSourceCount:(NSInteger)count{
    CGFloat realH = [QuizAnalyView getCollRealHeightWithDataSourceCount:count] + OtherViewH;
    return realH;
}

#pragma mark - UICollectionviewDelegate / DataSource
- (void)reloadDataWithDataSource:(NSArray *)dataSource{
    self.dataSource = [NSMutableArray arrayWithArray:dataSource];
    [self.collectionView reloadData];
}

- (void)reloadData{
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    QuizAnalysisCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    QuizAnalyListItem *item = [self.dataSource objectAtIndexSafe:indexPath.row];
    cell.item = item;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    QuizAnalyListItem *item = [self.dataSource objectAtIndexSafe:indexPath.row];
    return CGSizeMake(item.itemWidth, item.itemHeight);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(analyView:didSelectItem:atIndexPath:)]) {
        [self.delegate analyView:self didSelectItem:[self.dataSource objectAtIndexSafe:indexPath.row] atIndexPath:indexPath];
    }
}


#pragma mark - lazyload
- (UICollectionView *)collectionView{
    if(!_collectionView){
        QuizSubmitAnalysisViewFlowlayout *flow = [[QuizSubmitAnalysisViewFlowlayout alloc] init];
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        UICollectionView *collView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
        collView.backgroundColor = [UIColor clearColor];
        collView.delegate = self;
        collView.dataSource = self;
        collView.showsVerticalScrollIndicator = YES;
        [collView registerNib:[UINib nibWithNibName:NSStringFromClass([QuizAnalysisCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cellId];
        [self.contentView addSubview:collView];
        _collectionView = collView;
        
        __weak typeof(self)ws = self;
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(ws.contentView);
        }];
        flow.minimumInteritemSpacing = ItemM;
        flow.minimumLineSpacing = ItemM;
    }
    return _collectionView;
}



@end
